package com.esoon.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.esoon.smartsee.MainActivity;
import com.esoon.smartsee.R;
import com.esoon.utils.AlertDialogCommon;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;

public class FaceComfireActivity extends AppCompatActivity {
    private AlertDialogCommon recognizeDialog;
    private ImageView oldPicture;
    private ImageView newpicture;
    private SharedPreferences sp;
    private int screenWidth;
    private int screenHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_comfire);

        initView();


    }

    public static Bitmap imageScale(Bitmap bitmap, int dst_w, int dst_h) {
        int src_w = bitmap.getWidth();
        int src_h = bitmap.getHeight();
        float scale_w = ((float) dst_w) / src_w;
        float scale_h = ((float) dst_h) / src_h;
        Matrix matrix = new Matrix();
        matrix.postScale(scale_w, scale_h);
        Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix,
                true);
        return dstbmp;
    }

    private void initView() {
        if (sp == null) {

            sp = getSharedPreferences("smartsee", MODE_PRIVATE);

        }

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
// 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        screenHeight = (int) (height / density);// 屏幕高度(dp)


        oldPicture = (ImageView) findViewById(R.id.oldPicture);
        newpicture = (ImageView) findViewById(R.id.newPicture);
        File file = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
        if (file.exists()) {
            Bitmap bitmap = imageScale(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/temp.jpg"), screenWidth, screenHeight/2);
            newpicture.setImageBitmap(bitmap);
        }

        x.image().bind(oldPicture, "http://47.103.45.89:60000/operation/down?filePath=" + sp.getString("picId", "")
        );


        showComfiredDialog();
    }

    private void showComfiredDialog() {

        recognizeDialog = new AlertDialogCommon(FaceComfireActivity.this, R.layout.face_dialog);
        recognizeDialog.showDialog();
        WindowManager ctim = getWindowManager();
        Display ctid = ctim.getDefaultDisplay(); //为获取屏幕宽、高
        WindowManager.LayoutParams ctip = recognizeDialog.getWindow().getAttributes();//获取对话框当前的参数值
        ctip.height = ctid.getHeight() * 1; //高度设置为屏幕宽度
        ctip.width = ctid.getWidth() * 1; //宽度设置为屏幕长度
        recognizeDialog.getWindow().setAttributes(ctip); //设置生效
        Window ctidialogWindow = recognizeDialog.getWindow();
        WindowManager.LayoutParams ctilp = ctidialogWindow.getAttributes();
        Button submit = recognizeDialog.getView().findViewById(R.id.submitButton);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FaceComfireActivity.this, MainActivity.class);
                startActivity(intent);
                FaceComfireActivity.this.finish();
            }
        });

    }

}
